package com.gugee.tiktok.web.controller.web;


import com.gugee.tiktok.web.dto.RankSearchData;
import com.gugee.tiktok.web.service.RankingService;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Controller
@Slf4j
public class RankingsController extends TikTokWebController {


    @Autowired
    RankingService rankingService;

    RankSearchData searchData;

    @GetMapping("/ranking/influential")
    public ModelAndView influential(){
        searchData = new RankSearchData();
        Map<String,Object> ret = JdbcTemplateHelper.selectFirst("select max(belong) belong from ranking");
        searchData.setDate(Integer.parseInt(ret.get("belong").toString()));
        ModelAndView modelAndView = getModel(11);
        modelAndView.setViewName("ranking/influential");
        modelAndView.addObject("dataList", rankingService.getPageRanking(searchData,null));
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("title", "TikTok Influential Ranking");
        modelAndView.addObject("subTitle", "List of world top 1000 TikTok influencer leaderboard.");
        modelAndView.addObject("updateTime", rankingService.getRankingUpdateTime().get("daily"));
        return modelAndView;
    }

    @GetMapping("/ranking/follower")
    public ModelAndView follower(){
        searchData = new RankSearchData();
        Map<String,Object> ret = JdbcTemplateHelper.selectFirst("select max(belong) belong from ranking_followers");
        searchData.setDate(Integer.parseInt(ret.get("belong").toString()));
        ModelAndView modelAndView = getModel(12);
        modelAndView.setViewName("ranking/follower");
        modelAndView.addObject("dataList", rankingService.getPageRankingFollowers(searchData,null));
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("title", "TikTok Follower Counts Ranking");
        modelAndView.addObject("subTitle", "List of world top 1000 follower growth TikTok influencer leaderboard.");
        modelAndView.addObject("updateTime", rankingService.getRankingFollowerUpdateTime().get("daily"));
        return modelAndView;
    }

    @GetMapping("/ranking/enterprise")
    public ModelAndView enterprise(){
        searchData = new RankSearchData();
        Map<String,Object> ret = JdbcTemplateHelper.selectFirst("select max(belong) belong from ranking_enterprise");
        searchData.setDate(Integer.parseInt(ret.get("belong").toString()));
        ModelAndView modelAndView = getModel(13);
        modelAndView.setViewName("ranking/enterprise");
        modelAndView.addObject("dataList", rankingService.getPageRankingEnterprise(searchData,null));
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("title", "TikTok Enterprise Trends Ranking");
        modelAndView.addObject("subTitle", "List of world top 1000 enterprise account leaderboard.");
        modelAndView.addObject("updateTime", rankingService.getRankingEnterpriseUpdateTime().get("daily"));
        return modelAndView;
    }

    @GetMapping("/ranking/country")
    public ModelAndView country(){
        searchData = new RankSearchData();
        Map<String,Object> ret = JdbcTemplateHelper.selectFirst("select max(belong) belong from ranking_region");
        searchData.setDate(Integer.parseInt(ret.get("belong").toString()));
        ModelAndView modelAndView = getModel(14);
        modelAndView.addObject("dataList", rankingService.getPageRankingRegion(searchData,null));
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.addObject("title", "TikTok Country Follower Tracker Ranking");
        modelAndView.addObject("subTitle", "List of top 1000 TikTok influencer leaderboard in different countries.");
        modelAndView.addObject("updateTime", rankingService.getRankingRegionUpdateTime().get("daily"));
        return modelAndView;
    }





}
